package com.tomehay.termprojectmanage_server.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tomehay.termprojectmanage_server.enums.ResponseCode;
import com.tomehay.termprojectmanage_server.pojo.DTO.LoginDto;
import com.tomehay.termprojectmanage_server.pojo.Model.Teacher;
import com.tomehay.termprojectmanage_server.mapper.TeacherMapper;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tomehay
 * @since 2023-06-20
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {


    @Resource
    TeacherMapper teacherMapper;

    public ResultVO<Teacher> Login(LoginDto logindto){
        if(StringUtils.isEmpty(logindto.getAccount()) || StringUtils.isEmpty(logindto.getPassword())){
            return new ResultVO<>(ResponseCode.errorOf("用户名或密码不能为空!"));
        }

        logindto.setAccount(logindto.getAccount().trim());
        logindto.setPassword(logindto.getPassword().trim());

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id",logindto.getAccount());
        Teacher teacher = teacherMapper.selectOne(queryWrapper);

        if(teacher==null){
            return new ResultVO<>(ResponseCode.errorOf("用户不存在!"));
        }

        if(!teacher.getPassword().equals(logindto.getPassword())){
            return new ResultVO<>(ResponseCode.errorOf("密码错误"));
        }

        return new ResultVO<>(ResponseCode.OK,teacher);
    }
}
